/*  Source name		: hist_c.c
    Executable name	: hist_c
    Code model		: Protected-mode (32-bit C)
    Version		:
    Created date	:
    Last update		:
    Author		:
    Description		: This program outputs a histogram of total scores
			     and a frequency distribution of grades based
			     on a table of raw scores.
*/


#include <stdio.h>
#include "test2.h"


#define SIZE_SCORE_HIST		10
#define SIZE_GRADE_HIST		5


int	numrec = NUM_RECORDS;
int	num_scores[SIZE_SCORE_HIST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int	num_grades[SIZE_GRADE_HIST] = {0, 0, 0, 0, 0};
short   score_ranges[2*SIZE_SCORE_HIST] =	{   0,  50,
						   51, 100,
						  101, 150,
						  151, 200,
						  201, 250,
						  251, 300,
						  301, 350,
						  351, 400,
						  401, 450,
						  451, 500 };
char    grade_letters[SIZE_GRADE_HIST][2] = { "A", "B", "C", "D", "F" };

score_rec	*rec_ptr = NULL;


static	void compute_totals ();
static	void compute_grades ();
static	void create_hist ();
static	void print_results ();
static	void print_score_hist ();
static	void print_grade_hist ();


int main ()
{
    compute_totals ();
    compute_grades ();
    create_hist ();
    print_results ();

    exit (0);
}


void compute_totals ()
{
	rec_ptr = scores;
	int sum, i, j;
	for(i=0; i<NUM_RECORDS; ++i){
		sum = 0;
		for(j=0; j<NUM_SCORES; ++j){
			sum += rec_ptr->score[j];
		}
		rec_ptr->total = sum;
		++rec_ptr;
	}
}


void compute_grades ()
{
	rec_ptr = scores;
	int i, j;
	for(i=0; i<NUM_RECORDS; ++i){
		for(j=4; j>=0; --j){
			if(100*j < rec_ptr->total && 100*j+100 >= rec_ptr->total)
				rec_ptr->grade = grade_letters[4-j][0];

		} 
		++rec_ptr;
	}
}


void create_hist ()
{
	rec_ptr = scores;
	int i, j;
	for(i=0; i<NUM_RECORDS; ++i){
		for(j=9; j>=0; --j){
			if(50*j < rec_ptr->total && 50*j+50 >= rec_ptr->total){
				++(num_scores[j]);
			}
			if((j & 1) == 0 && (j>>1)*100 < rec_ptr->total && 100*(j>>1)+100 >= rec_ptr->total)
				++num_grades[4-(j>>1)];
		}
		++rec_ptr;
	}
}


void print_results ()
{
    print_score_hist ();

    printf ("\n");

    print_grade_hist ();
}


void print_score_hist ()
{
    int		i, j;

    for (i = 0; i < SIZE_SCORE_HIST; i++)
    {
	printf ("%3d - %3d: ", score_ranges[2*i], score_ranges[2*i+1]);

	for (j = 0; j < num_scores[i]; j++)
	    printf ("*");

	printf ("\n");
    }
}


void print_grade_hist ()
{
    int		i;

    for (i = 0; i < SIZE_GRADE_HIST; i++)
    {
	printf ("Number of %s's:  %d\n", grade_letters[i], num_grades[i]);
    }
}

